<?php

declare(strict_types=1);

namespace app\index\middleware;

use app\model\Salesman;
use think\facade\Request;

class Auth
{
    public function handle($request, \Closure $next)
    {
        if ($request->pathinfo() === 'user/login') {
            return $next($request);
        }

        $authorization = Request::header('Authorization', 'Bearer ');

        $token = str_replace('Bearer ', '', $authorization);

        $request->user = Salesman::where('token', $token)->findOrEmpty();

        if ($request->user->isEmpty()) {
            return json([
                'code' => 40401,
                'msg' => '用户未登录'
            ]);
        }

        return $next($request);
    }
}
